//
//  CurrencyExchangeGmoTester.m
//  DataCollector
//
//  Created by jiro on 12/07/14.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "CurrencyExchangeGmoTester.h"

#import "../../Commons.h"

double error  = 0.00001;

@implementation CurrencyExchangeGmoTester

+ (void) testAll
{
	[ CurrencyExchangeGmoTester testQueriesSelectAllWithCurrencyCode000 ];
	[ CurrencyExchangeGmoTester testQueriesSelectAllWithCurrencyCode001 ];
	[ CurrencyExchangeGmoTester testQueriesSelectAllWithCurrencyCode002 ];
	[ CurrencyExchangeGmoTester testQueriesSelectAllWithCurrencyCode003 ];
	[ CurrencyExchangeGmoTester testQueriesSelectAllWithCurrencyCode004 ];
}

+ (void) testQueriesSelectAllWithCurrencyCode000
{
	id ope = [ DbOperator defaultOperator ];
	NSArray* s;
	std::vector< PGresult* > r;
	
	id startDate = [ NSDate dateWithString: @"2011-3-11 00:00:00 +0900" ];
	id endDate = [ NSDate dateWithString: @"2011-3-11 01:00:00 +0900" ];
	s = [ CurrencyExchangeGmo
			queriesSelectAllWithCurrencyCode: @"USD" targetCurrencyCode: @"JPY"
			startDate: startDate endDate: endDate
			andIntervalSec: secondsForOneHour ];
	AssertEqual( [ s count ], 2, @"%d" );
	if ( DbResponse_Success != [ ope executeQueries: s intoResults: r ] )
	{
		AssertTrue( NO );
	}
	id list = [ CurrencyExchangeJaList listWithPgResults: r ];

	CurrencyExchangeGmo* gmo = [ list objectAtIndex: 0 ];
	AssertTrue( [ gmo.exchangeDate isEqualToDate: startDate ] );
	AssertTrue( fabs( gmo.openPrice - 83.044 ) < error );
	AssertTrue( fabs( gmo.highPrice - 83.08 ) < error );
	AssertTrue( fabs( gmo.lowPrice - 82.929 ) < error );
	AssertTrue( fabs( gmo.closePrice - 82.957 ) < error );

	gmo = [ list objectAtIndex: 1 ];
	AssertTrue( [ gmo.exchangeDate isEqualToDate: endDate ] );
	AssertTrue( fabs( gmo.openPrice - 82.958 ) < error );
	AssertTrue( fabs( gmo.highPrice - 83.1 ) < error );
	AssertTrue( fabs( gmo.lowPrice - 82.944 ) < error );
	AssertTrue( fabs( gmo.closePrice - 83.031 ) < error );
	
}

+ (void) testQueriesSelectAllWithCurrencyCode001
{
	id ope = [ DbOperator defaultOperator ];
	std::vector< PGresult* > r;
	id s = [ CurrencyExchangeGmo 
		queriesSelectAllWithCurrencyCode: @"USD" targetCurrencyCode: @"JPY" 
		startDate: [ NSDate dateWithString: @"2011-3-12 07:04:00 +0900" ] endDate: [ NSDate dateWithString: @"2011-3-12 07:05:00 +0900" ]
		andIntervalSec: 5 * secondsForOneMinuet ];
	if ( DbResponse_Success != [ ope executeQueries: s intoResults: r ] )
	{
		AssertTrue( NO );
	}
	AssertEqual( [ s count ], 1, @"%d" );
	id list = [ CurrencyExchangeJaList listWithPgResults: r ];
	AssertEqual( [ list count ], 0, @"%d" );
	
	[ ope disconnect ]; 
}

+ (void) testQueriesSelectAllWithCurrencyCode002
{
	id ope = [ DbOperator defaultOperator ];
	id startDate = [ NSDate dateWithString: @"2011-3-12 07:03:00 +0900" ];
	id endDate = [ NSDate dateWithString: @"2011-3-12 07:05:00 +0900" ];
	
	std::vector< PGresult* > r;
	id s = [ CurrencyExchangeGmo 
		queriesSelectAllWithCurrencyCode: @"USD" targetCurrencyCode: @"JPY" 
		startDate: startDate endDate: endDate
		andIntervalSec: 5 * secondsForOneMinuet ];
	if ( DbResponse_Success != [ ope executeQueries: s intoResults: r ] )
	{
		AssertTrue( NO );
	}
	AssertEqual( [ s count ], 1, @"%d" );
//	NSLog( @"%@", [ s objectAtIndex: 0 ] );
	id list = [ CurrencyExchangeJaList listWithPgResults: r ];
	AssertEqual( [ list count ], 1, @"%d" );
	CurrencyExchangeGmo* gmo = [ list objectAtIndex: 0 ];
	AssertTrue( [ gmo.exchangeDate isEqualToDate: [ NSDate dateWithString: @"2011-3-12 06:59:00 +0900" ] ] );
	AssertTrue( fabs( gmo.openPrice - 81.84 ) < error );
	AssertTrue( fabs( gmo.highPrice - 81.846 ) < error );
	AssertTrue( fabs( gmo.lowPrice - 81.815 ) < error );
	AssertTrue( fabs( gmo.closePrice - 81.846 ) < error );

	[ ope disconnect ]; 
}

+ (void) testQueriesSelectAllWithCurrencyCode003
{
}

+ (void) testQueriesSelectAllWithCurrencyCode004
{
}


@end
